package com.wcy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcy.annotation.aop.PreAuth;
import com.wcy.entity.UserComment;
import com.wcy.entity.queryVo.QueryConvertor;
import com.wcy.service.IUserCommentService;
import com.wcy.util.JwtTokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/userComment")
public class UserCommentController {

    @Autowired
    private IUserCommentService userCommentService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @PreAuth("COMMENT:QUERY")
    @GetMapping("/page")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Page<UserComment>> getPage(@RequestParam(defaultValue = "1") int pageNum,
                                                     @RequestParam(defaultValue = "10") int pageSize,
                                                     HttpServletRequest request) {
        String queryData = request.getHeader("X-Query-Data");
        QueryWrapper wrapper = new QueryConvertor(queryData).convert(true);
        Page<UserComment> page = userCommentService.page(new Page<>(pageNum, pageSize),wrapper);
        return ResponseEntity.ok(page);
    }

    @PreAuth("AUTHOR:ENTRY")
    @PostMapping("/byResourceId/page")
    public ResponseEntity<Page<UserComment>> getByResourceIdsPage(@RequestParam(defaultValue = "1") int pageNum,
                                                                  @RequestParam(defaultValue = "10") int pageSize,
                                                                  @RequestParam("resourceIds") List<Integer> resourceIds,
                                                                  HttpServletRequest request){
        String queryData = request.getHeader("X-Query-Data");
        QueryWrapper wrapper = new QueryConvertor(queryData,true).convert(true);
        wrapper.in("resource_id", resourceIds);
        return ResponseEntity.ok(userCommentService.page(new Page<>(pageNum, pageSize),wrapper));
    }

    @PreAuth("COMMENT:QUERY")
    @GetMapping("/list")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<List<UserComment>> getList(HttpServletRequest request) {
        String queryData = request.getHeader("X-Query-Data");
        QueryWrapper wrapper = new QueryConvertor(queryData).convert(true);
        List<UserComment> list = userCommentService.list(wrapper);
        return ResponseEntity.ok(list);
    }

    @PreAuth("COMMENT:ADD")
    @PostMapping("/add")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Void> add(@RequestBody UserComment userComment) {
        userComment.setCommentAt(LocalDateTime.now());
        userCommentService.save(userComment);
        return ResponseEntity.ok().build();
    }

    @PreAuth("COMMENT:ALTER")
    @PutMapping("/update")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Void> update(@RequestBody UserComment userComment) {
        userCommentService.updateById(userComment);
        return ResponseEntity.ok().build();
    }

    @PreAuth("COMMENT:DELETE")
    @DeleteMapping("/delete/{id}")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Void> delete(@PathVariable Integer id) {
        userCommentService.removeById(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/resource/{id}")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Page<UserComment>> getByResourceId(@PathVariable("id") Integer id,
                                                             @RequestParam(name = "pageNum",defaultValue = "1") int pageNum,
                                                             @RequestParam(name = "pageSize",defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(userCommentService.page(Page.of(pageNum,pageSize),new QueryWrapper<UserComment>().eq("resource_id", id).orderBy(true,false,"comment_at")));
    }

    @GetMapping("/user")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Page<UserComment>> getBySelf(@RequestParam(name = "pageNum",defaultValue = "1") int pageNum,
                                                             @RequestParam(name = "pageSize",defaultValue = "10") int pageSize,
                                                             HttpServletRequest request) {
        Integer id = jwtTokenUtil.getUserIdFromToken(jwtTokenUtil.getToken(request));
        String queryData = request.getHeader("X-Query-Data");
        QueryWrapper wrapper = new QueryConvertor(queryData,true).convert(true);
        wrapper.eq("user_id", id);
        return ResponseEntity.ok(userCommentService.page(Page.of(pageNum,pageSize),wrapper));
    }

    @PostMapping("/sendComment")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Void> sendComment(@RequestBody UserComment userComment,HttpServletRequest request) {
        Integer id = jwtTokenUtil.getUserIdFromToken(jwtTokenUtil.getToken(request));
        userComment.setCommentId(0);
        userComment.setUserId(id);
        userComment.setCommentAt(LocalDateTime.now());
        System.out.println(userComment);
        userCommentService.save(userComment);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("user/delete/{id}")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Void> deleteByUser(@PathVariable("id") Integer id,HttpServletRequest request) {
        Integer userId = jwtTokenUtil.getUserIdFromToken(jwtTokenUtil.getToken(request));
        userCommentService.remove(new QueryWrapper<UserComment>().eq("comment_id", id).eq("user_id", userId));
        return ResponseEntity.ok().build();
    }
}